<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-if="hasPermission('2-2-add')">
          新增角色
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                label: '编辑',
                ifShow: hasPermission('2-2-update'),
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                label: '删除',
                ifShow: hasPermission('2-2-del'),
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { usePermission } from '/@/hooks/web/usePermission';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  // import { getRoleListByPage } from '/@/api/demo/system';
  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from '/@/views/project/system/role/RoleDrawer.vue';
  import { columns } from '/@/views/project/system/role/role.data';
  import { queryroles, deleterole } from '/@/api/system/role';
  import { onMounted } from 'vue';

  const { hasPermission } = usePermission();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getPaginationRef }] = useTable({
    title: '角色列表',
    api: queryroles,
    columns,
    immediate: false,
    bordered: true,
    showIndexColumn: false,
    actionColumn: hasPermission(['2-2-update', '2-2-del'])
      ? {
          width: 150,
          title: '操作',
          dataIndex: 'action',
        }
      : undefined,
    fetchSetting: {
      pageField: 'CurrentPage',
      sizeField: 'PageSize',
      totalField: 'TotalCount',
      listField: 'Items',
    },
    beforeFetch: () => {
      return [getPaginationRef().current || 1, getPaginationRef().pageSize || 50];
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record) {
    await deleterole([record.Name]);
    reload();
  }

  function handleSuccess() {
    reload();
  }

  onMounted(() => {
    reload();
  });
</script>
<script lang="ts">
  export default {
    name: 'SystemRole',
  };
</script>
